package com.huike.service.impl;

import com.huike.entity.Admin;
import com.huike.entity.Policy;
import com.huike.mapper.PolicyMapper;
import com.huike.service.IPolicyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lwen
 * @since 2019-11-12
 */
@Service
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, Policy> implements IPolicyService {

	@Override
	public IPage<Policy> listByPage(Admin admin, int page, int limit) {
		QueryWrapper<Policy> wrapper = new QueryWrapper<>();
		Page<Policy> pages = new Page<>(page, limit);
		if("管理员".equals(admin.getGride())){
			wrapper.eq("belong_user", admin.getUid());
		}
		wrapper.orderByDesc("time"); // 增加时间逆序条件
		IPage<Policy> iPage = baseMapper.selectPage(pages, wrapper);
		return iPage;
	}
	
	@Override
	public IPage<Policy> listByPage(int page, int limit) {
		QueryWrapper<Policy> wrapper = new QueryWrapper<>();
		Page<Policy> pages = new Page<>(page, limit);
		wrapper.orderByDesc("time"); // 增加时间逆序条件
		IPage<Policy> iPage = baseMapper.selectPage(pages, wrapper);
		return iPage;
	}

	@Override
	public boolean insertPolicy(Admin admin, Policy policy) {
		policy.setBelongUser(admin.getUid());
		int res = baseMapper.insert(policy);
		if (res == 1) {
			return true;
		}
		return false;
	}

	@Override
	public Boolean deletePolicy(List<String> ids) {
		int res = baseMapper.deleteBatchIds(ids);
		if (res == 1) {
			return true;
		}
		return false;
	}

}
